<template>
    <div class="login-wrap">
        <div class="ms-title">{{plat.name}}</div>
        <div class="ms-login">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">
                <el-form-item prop="username">
                    <el-input v-model="ruleForm.username" placeholder="username"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" placeholder="password" v-model="ruleForm.password" @keyup.enter.native="submitForm('ruleForm')"></el-input>
                </el-form-item>
                <el-form-item prop="captcha">
                    <el-input style="width:120px;" placeholder="验证码" v-model="ruleForm.captcha" @keyup.enter.native="submitForm('ruleForm')"></el-input>
                    <img style="vertical-align: middle;" :src="captchaUrl" @click="getCaptchaUrl"/>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
    import util from '../common/util.js';
    export default {
        data: function(){
            return {
                plat:{
                    name:'短信平台'
                },
                ruleForm: {
                    username: null,
                    password: null,
                    captcha: null
                },
                rules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' }
                    ],
                    captcha: [
                        { required: true, message: '请输入验证码', trigger: 'blur' }
                    ]
                },
                captchaId:new Date().getTime(),
                captchaUrl:null
            }
        },
        created() {
            util.restful('get','plat',null,(plat) => {
               this.plat.name = plat.name;
            });
            this.getCaptchaUrl();
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        util.restful('post','tokens',{accountId: this.ruleForm.username, password: this.ruleForm.password, captcha: this.ruleForm.captcha,captchaId:this.captchaId,loginMethod:"common"},(result) => {
                            if (result.status == "OK") {
                                localStorage.setItem("loggedAccountId",result.data.userId);
                                localStorage.setItem("token",result.data.token);
                                this.$router.push('/');
                            }
                        });
                    }
                });
            },
            getCaptchaUrl() {
                this.captchaUrl = util.apiBaseUrl+'captcha?id='+this.captchaId+'&v='+Math.random();
            }
        }
    }
</script>

<style scoped>
    .login-wrap{
        position: relative;
        width:100%;
        height:100%;
    }
    .ms-title{
        position: absolute;
        top:50%;
        width:100%;
        margin-top: -230px;
        text-align: center;
        font-size:30px;
        color: #fff;

    }
    .ms-login{
        position: absolute;
        left:50%;
        top:50%;
        width:300px;
        height:160px;
        margin:-150px 0 0 -190px;
        padding:40px;
        border-radius: 5px;
        background: #fff;
    }
    .login-btn{
        text-align: center;
    }
    .login-btn button{
        width:100%;
        height:36px;
    }
</style>
